package com.bcu.cn.onlineexamsystem.studentmanage.controller;

import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentProfileService;
import com.bcu.cn.onlineexamsystem.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/student/profile")
public class StudentProfileController {
    
    @Autowired
    private StudentProfileService studentProfileService;
    
    /**
     * 学生个人信息页面
     */
    @GetMapping("/manage")
    public String profile(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null || !"student".equals(user.getRole())) {
            return "redirect:/login";
        }
        
        // 获取学生个人信息
        StudentProfileDTO profile = studentProfileService.getStudentProfile(user.getUserId());
        model.addAttribute("profile", profile);
        model.addAttribute("user", user);
        
        return "student/profile";
    }
    
    /**
     * 更新邮箱
     */
    @PostMapping("/updateEmail")
    @ResponseBody
    public Map<String, Object> updateEmail(@RequestParam String email, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        User user = (User) session.getAttribute("user");
        if (user == null || !"student".equals(user.getRole())) {
            result.put("success", false);
            result.put("message", "用户未登录或权限不足");
            return result;
        }
        
        try {
            boolean success = studentProfileService.updateEmail(user.getUserId(), email);
            result.put("success", success);
            if (success) {
                result.put("message", "邮箱更新成功");
                // 更新session中的用户信息
                user.setEmail(email);
                session.setAttribute("user", user);
            } else {
                result.put("message", "邮箱格式不正确或更新失败");
            }
        } catch (Exception e) {
            System.err.println("更新邮箱失败: " + e.getMessage());
            result.put("success", false);
            result.put("message", "服务器错误，请稍后重试");
        }
        
        return result;
    }
    
    /**
     * 更新电话号
     */
    @PostMapping("/updatePhone")
    @ResponseBody
    public Map<String, Object> updatePhone(@RequestParam String phone, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        User user = (User) session.getAttribute("user");
        if (user == null || !"student".equals(user.getRole())) {
            result.put("success", false);
            result.put("message", "用户未登录或权限不足");
            return result;
        }
        
        try {
            boolean success = studentProfileService.updatePhone(user.getUserId(), phone);
            result.put("success", success);
            if (success) {
                result.put("message", "电话号更新成功");
                // 更新session中的用户信息
                user.setPhone(phone);
                session.setAttribute("user", user);
            } else {
                result.put("message", "电话号格式不正确或更新失败");
            }
        } catch (Exception e) {
            System.err.println("更新电话号失败: " + e.getMessage());
            result.put("success", false);
            result.put("message", "服务器错误，请稍后重试");
        }
        
        return result;
    }
    
    /**
     * 获取个人信息数据（AJAX接口）
     */
    @GetMapping("/data")
    @ResponseBody
    public StudentProfileDTO getProfileData(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !"student".equals(user.getRole())) {
            return null;
        }
        
        return studentProfileService.getStudentProfile(user.getUserId());
    }
} 